package ops.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

import ops.controller.ControllerManager;
import ops.i18n.Resources;

public class MenuBar extends JMenuBar {
	
	private enum MenuType {
		SIMPLE_ITEM, CHECKBOX_ITEM, RADIOBOX_ITEM;
	}
	
	private JMenuItem[] studentAccessMenu;
	private JMenuItem[] adminAccessMenu;
	private JMenuItem[] commonAccessMenu;
	
	private MenuListener menuListener;
	
	private JMenu menuLogin;
	private JMenuItem itemLogin;
	private JMenuItem itemLogout;

	private JMenu  menuRegister;
	private JMenuItem itemStudents;
	private JMenuItem itemSubjects;
	private JMenuItem itemClasses;
	private JMenuItem itemTeachers;
	private JMenuItem itemCourses;
	private JMenuItem itemTerm;

	private JMenu  menuSubscription;
	private JMenuItem itemSubscription;
	private JMenuItem itemManageSubscription;

	private JMenu  menuGrades;
	private JMenuItem itemAddGrades;

	private JMenu  menuReport;
	private JMenuItem itemCRID;
	private JMenuItem itemBoletim;
	private JMenuItem itemPauta;

	public MenuBar() {
		super();
		
		menuListener = new MenuListener();
		
		init();
		initPerms();
		
		changePermissionCommons(true);
	}

	private void initPerms() {
		commonAccessMenu = new JMenuItem[] {
				itemLogin, itemLogout
		};
		
		studentAccessMenu = new JMenuItem[] {
				itemSubscription, itemCRID, itemBoletim, itemPauta
		};
		
		adminAccessMenu = new JMenuItem[] {
				itemStudents, itemSubjects, itemClasses, itemTeachers,
				itemCourses, itemTerm, itemManageSubscription, itemAddGrades,
		};
	}
	
	public void changePermissionCommons(boolean allow) {
		for (JMenuItem item : commonAccessMenu) {
			item.setEnabled(allow);
		}
	}
	
	public void changePermissionStudent(boolean allow) {
		for (JMenuItem item : studentAccessMenu) {
			item.setEnabled(allow);
		}
	}
	
	public void changePermissionAdmin(boolean allow) {
		for (JMenuItem item : adminAccessMenu) {
			item.setEnabled(allow);
		}
	}

	private void init() {
		menuLogin = new JMenu(getLocalizedString("menu.login"));
		menuLogin.setMnemonic('L');
		add(menuLogin);
		
		itemLogin = createMenuItem(menuLogin, MenuType.SIMPLE_ITEM, "login.login");
		itemLogout = createMenuItem(menuLogin, MenuType.SIMPLE_ITEM, "login.logout");
		itemLogout.addActionListener(new LogoutListener());

		menuRegister = new JMenu(getLocalizedString("menu.register"));
		menuRegister.setMnemonic('C');
		add(menuRegister);
		
		itemStudents = createMenuItem(menuRegister, MenuType.SIMPLE_ITEM, "register.students");
		itemSubjects = createMenuItem(menuRegister, MenuType.SIMPLE_ITEM, "register.subjects");
		itemClasses = createMenuItem(menuRegister, MenuType.SIMPLE_ITEM, "register.classes");
		itemTeachers = createMenuItem(menuRegister, MenuType.SIMPLE_ITEM, "register.teachers");
		itemCourses = createMenuItem(menuRegister, MenuType.SIMPLE_ITEM, "register.courses");
		itemTerm = createMenuItem(menuRegister, MenuType.SIMPLE_ITEM, "register.term");

		menuSubscription = new JMenu(getLocalizedString("menu.subscription"));
		menuSubscription.setMnemonic('I');
		add(menuSubscription);
		
		itemSubscription= createMenuItem(menuSubscription, MenuType.SIMPLE_ITEM, "subscription.student");
		itemManageSubscription= createMenuItem(menuSubscription, MenuType.SIMPLE_ITEM, "subscription.manage");

		menuGrades = new JMenu(getLocalizedString("menu.grades"));
		menuGrades.setMnemonic('N');
		add(menuGrades);
		
		itemAddGrades = createMenuItem(menuGrades, MenuType.SIMPLE_ITEM, "grades.add");

		menuReport = new JMenu(getLocalizedString("menu.report"));
		menuReport.setMnemonic('R');
		add(menuReport);
		
		itemCRID = createMenuItem(menuReport, MenuType.SIMPLE_ITEM, "report.crid");
		itemBoletim = createMenuItem(menuReport, MenuType.SIMPLE_ITEM, "report.card");
		itemPauta = createMenuItem(menuReport, MenuType.SIMPLE_ITEM, "report.list");
	}

	private String getLocalizedString(String key) {
		return Resources.getInstance().getString(key);
	}
	
	public JMenuItem createMenuItem(JMenu menu, MenuType type, String id) {
		JMenuItem menuItem = null;
		String key = "menu."+id;

		if (type == MenuType.RADIOBOX_ITEM) {
			menuItem = new JRadioButtonMenuItem();
		} else if (type == MenuType.CHECKBOX_ITEM) {
			menuItem = new JCheckBoxMenuItem();
		} else if (type == MenuType.SIMPLE_ITEM) {
			menuItem = new JMenuItem();
		}
		
		menuItem.setText(getLocalizedString(key));

		// icon, optional
		String imagePath = getLocalizedString(key+".icon");
		if (imagePath != null && !imagePath.equals("")) {
			menuItem.setIcon(new ImageIcon(imagePath));
		}		

		// mnemonic, optional
		String mnemonicString = getLocalizedString(key+".mnemonic");
		if (mnemonicString != null && !mnemonicString.equals("")) {
			menuItem.setMnemonic(mnemonicString.charAt(0));
		}
		
		// tooltip, optional
		String toolTip = getLocalizedString(key+".tooltip");
		if(toolTip != null && !toolTip.equals("")) {
			menuItem.setToolTipText(toolTip);
		}

		menuItem.setActionCommand(id);

		menuItem.addActionListener(menuListener);
		
		menuItem.setEnabled(false);
		
		menu.add(menuItem);
		
		return menuItem;
	}
	
	private class MenuListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			JMenuItem item = (JMenuItem) e.getSource();
			if (!item.isEnabled()) {
				return;
			}			
			
			String cmd = e.getActionCommand();
			
			ApplicationFrame.getInstance().showPanel(cmd);
		}
		
	}

	private class LogoutListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			ControllerManager.getInstance().getLoginController().logout();
		}
	}
}
